#############################
# Computes './data/interpolation 2010 2005/turnout_2010_dasymetric_interpolation.csv'
# used in Appendix Table B.6
##################################

# load required packages
require(foreign)
require(maptools)
require(raster)
require(readxl)
require(rgdal)
require(rgeos)
require(gdalUtils)
require(reshape2)
require(data.table)
require(stringr)


#Confirm the GDAL installation is found; the interpolation programme will
#verify this but as it is time-consuming, it makes sense to do it here.

gdal_setInstallation()

#Load the Interpolation Programme
source('./code/interpolation 2010 2005/interpolation_programme.R')

#Load Elections Data
df.2010 <- read_xlsx('./data/candidates/2010/British_Parliamentary_Constituency_General_Election_2010_Version_5.xlsx', sheet=1)
df.2010 <- data.frame(df.2010)[c('RefNo','Seat','Convt10','Labvt10','LDvt10','Turn10','Elec10')]
names(df.2010) <- c('pa','seat','con_vote','lab_vote','lib_vote','turnout','electorate')
df.2010$turnout <- round((((df.2010$turnout)/100)*df.2010$electorate))
setDT(df.2010)

#add ons id
id <- fread(file = './data/candidates/2010/pa_ons_id.csv')
id <- id[,c("Press Association ID Number","Constituency Name","Constituency ID")]
df.2010 <- merge(df.2010, id, by.x='pa', by.y='Press Association ID Number', all.x=TRUE)


#ward population data
res.2011 <- read.csv('./data/interpolation 2010 2005/Data_UNIT_URESPOP.csv')[-1,]
res.2011 <- res.2011[c('GEO_CODE','F2384')]
names(res.2011) <- c('geo_code', 'pop_2011')

#Load the Shapefiles
#Census Shapefiles with WARDs
pop.2011 <- readShapeSpatial('./data/interpolation 2010 2005//infuse_ward_lyr_2011/infuse_ward_lyr_2011.shp')

pop.2011@data <- data.frame(
  pop.2011@data, res.2011[match(pop.2011$geo_code, res.2011$geo_code),])
# remove N.Ireland
pop.2011 <- pop.2011[!is.na(pop.2011@data$pop_2011), ]
pop.2011@data$pop_2011 <- as.numeric(pop.2011@data$pop_2011)

#Shapefiles for the elections
cty.2010 <- readShapeSpatial('./data/Westminster_Parliamentary_Constituencies_(Dec_2017)_UK_BFE/Westminster_Parliamentary_Constituencies_(Dec_2017)_UK_BFE.shp')
cty.2010@data$OBJECTID <- str_pad(cty.2010@data$OBJECTID, 3, pad = "0")

#Merge the data into the shapefiles
cty.2010@data <- data.frame(
  cty.2010@data, df.2010[match(cty.2010$PCON17CD, df.2010$`Constituency ID`),])
cty.2010@data$BNG_E <- as.character(cty.2010@data$BNG_E)
cty.2010@data$BNG_N <- as.character(cty.2010@data$BNG_N)
cty.2010@data$LONG <- as.character(cty.2010@data$LONG)
cty.2010@data$LAT <- as.character(cty.2010@data$LAT)
cty.2010@data$`Shape__Are` <- as.character(cty.2010@data$`Shape__Are`)
cty.2010@data$`Shape__Len` <- as.character(cty.2010@data$`Shape__Len`)
cty.2010@data$pa <- as.character(cty.2010@data$pa)
cty.2010 <- cty.2010[grep('^(E|W|S)',cty.2010@data$PCON17CD), ]

cty.2005 <- readShapeSpatial('./data/interpolation 2010 2005//Westminster_Parliamentary_Constituencies_2005/PCON_DEC_2005_GB_BFE.shp')

#Interpolations
#Note that lines 77-83 take about 6hrs to run on a MacBook Pro Corei7 16GB
#Output data is in './data/interpolation 2010 2005/vote_interpolation_2010_2005.rds' and can be load in line 87

constant.raster.size <- 6000

int.2005 <- interpolation(
  new.shp = cty.2005, new.name = 'PCON05CD',
  old.shp = cty.2010, old.name = 'PCON17CD',
  pop.shp = pop.2011, pop.name = 'pop_2011',
  use.gdal=TRUE, raster.size=constant.raster.size)


#saveRDS(int.2005, file = './data/interpolation 2010 2005/vote_interpolation_2010_2005.rds')
#int.2005 <- readRDS('./data/interpolation 2010 2005/vote_interpolation_2010_2005.rds')

turnout2010 <- merge(int.2005$int.di, cty.2005@data, by='PCON05CD')
fwrite(turnout2010, file = './data/interpolation 2010 2005/turnout_2010_dasymetric_interpolation.csv')


